package com.lee.dutylower.view.detail;

import android.app.Fragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;

import com.lee.dutylower.R;
import com.lee.dutylower.db.greenEntity.Times;
import com.lee.dutylower.utils.LogUtils;


/**
 * Created by lijie on 2017/5/11.
 */

public class TimeDetailFragment extends Fragment {
    private Times timesDB;
    private boolean isEditable;
    public boolean isAdd = false;
    public String sTimeStart = "";
    public String sTimeEnd = "";
    private Button btnStartTime;
    private Button btnEndTime;
    private TextView tvStart;
    private TextView tvEnd;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle b = getArguments();
        if (b != null){
            timesDB = (Times) b.getSerializable("TimesDB");
            isEditable = getArguments().getBoolean("isEditable",false);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View v = View.inflate(getActivity(), R.layout.fragment_detail_time,null);
        btnStartTime = (Button) v.findViewById(R.id.btn_start_time);
        btnEndTime = (Button) v.findViewById(R.id.btn_end_time);
        tvStart = (TextView) v.findViewById(R.id.tv_start_time);
        tvEnd = (TextView) v.findViewById(R.id.tv_end_time);

        if(timesDB == null){
            initEditableView(null);
            isAdd = true;
        }else {
            sTimeStart = timesDB.getStartTime();
            sTimeEnd = timesDB.getEndTime();
            dataNotNull();
            isAdd = false;
        }
        btnStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        String h,m;
                        if (hourOfDay <10){
                            h = "0"+hourOfDay;
                        }else {
                            h = hourOfDay+"";
                        }
                        if (minute <10){
                            m = "0"+minute;
                        }else {
                            m = minute+"";
                        }
                        sTimeStart = h+":"+m;
                        tvStart.setText(sTimeStart);
                        LogUtils.e("TimeDetailFragment",sTimeStart);
                    }
                }, 0, 0, true).show();
            }
        });
        btnEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new TimePickerDialog(getActivity(), new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                        String h,m;
                        if (hourOfDay <10){
                            h = "0"+hourOfDay;
                        }else {
                            h = hourOfDay+"";
                        }
                        if (minute <10){
                            m = "0"+minute;
                        }else {
                            m = minute+"";
                        }
                        sTimeEnd = h+":"+m;
                        tvEnd.setText(sTimeEnd);
                        LogUtils.e("TimeDetailFragment",sTimeEnd);
                    }
                }, 0, 0, true).show();
            }
        });

        return v;
    }

    public void dataNotNull() {

        if(isEditable){
            initEditableView(timesDB.getStartTime()+"-"+timesDB.getEndTime());
        }else {
            btnStartTime.setEnabled(false);
            btnEndTime.setEnabled(false);
            tvStart.setText(timesDB.getStartTime());
            tvEnd.setText(timesDB.getEndTime());
        }
    }


    //在条件为可编辑时进行 是否新增 判断处理
    private void initEditableView(String name) {
        btnStartTime.setEnabled(true);
        btnEndTime.setEnabled(true);
        if(name == null){
            tvStart.setText("无开始时间");
            tvEnd.setText("无结束时间");
        }else {
            tvStart.setText(timesDB.getStartTime());
            tvEnd.setText(timesDB.getEndTime());
        }
    }
}
